package com.wave2.shadow.testJavaGolfAssocation;

import static org.junit.Assert.*;
import org.junit.Test;

import org.easymock.EasyMock;

import com.wave2.shadow.dao.RegistrationDao;
import com.wave2.shadow.exceptions.UserAlreadyExists;
import com.wave2.shadow.model.JoinForm;
import com.wave2.shadow.services.JGARegistrationService;

public class TestRegistration{
	
	// EasyMock example referenced from: http://www.ibm.com/developerworks/java/library/j-easymock/index.html 
	
	@Test
	public void testRegistration(){
		//Mocking performed using EasyMock 3 required libs: objenesis-1.2.jar, cglib-2.2.2.jar, easymock-3.1.jar
		RegistrationDao mockDAO = EasyMock.createMock(RegistrationDao.class);		//mock out the DAO layer so we no longer need the database to test the service.
		JoinForm mockJoinForm = EasyMock.createMock(JoinForm.class);			//mock out the JoinFormData
		
		try {
			JGARegistrationService jgaRegistrationService = new JGARegistrationService();
			
			jgaRegistrationService.setRegistrationDao(mockDAO);
			jgaRegistrationService.createUser(mockJoinForm);
			
		} catch (UserAlreadyExists e) {
			
			e.printStackTrace(System.err);
			fail("JUnit FAILED! DAO returned UserAlreadyExists exception and shouldn't have.");
			
		} catch (Exception e){
			
			e.printStackTrace(System.err);
			fail("JUnit FAILED! Unhandled Exception!");
			
		}
	}
	
	//TODO: Figure out why the EasyMock isn't throwing the exception. Exception gets thrown in a system run but not in the unit test.
	/*
	@Test
	public void testUserExistsException(){
		JGARegistrationService service = EasyMock.createMock(JGARegistrationService.class);
		JoinForm user = EasyMock.createMock(JoinForm.class);	
		
		try {
			
			EasyMock.expect(service.createUser(user)).andThrow(new UserAlreadyExists());			
			
			//we should never get passed the prior EasyMock.expect
			fail("JGARegistrationService should have thrown a UserAlreadyExists exception but did not.");
			
		} catch (UserAlreadyExists e) {
			
			//Unit test PASS if we hit this exception
			e.printStackTrace(System.err);
			
		} catch (Exception e) {
			
			e.printStackTrace(System.err);
			fail("JGARegistrationService Unhandled exception! This should not have happened!");
			
		}
		
	}
	*/
}
